const Com = require("../gmoon/baseCom");
cc.Class({
    extends: Com,

    properties: {
    },

    ctor(){
        this._lev = 0;
        this._killv = 0;
    },

    onLoad(){
        //$EBUS.on("gun-moving-evnet" , this.onGunMoving.bind(this) );
        //console.log( $T.getScr(this.node) )
    },

    start () {

    },

    setInfo( p ){
        p = p || {};
        if(p.killv){
            this._killv = p.killv;
        }
    },

    getKillv(){
        return this._killv;
    },

    onGunMoving( e ){
        let d = e.getUserData();
        console.log( 'zidan onGunMoving' );
        d.callback && d.callback();
    },

    setSize( w , h){
        return $T.setNodeSize(this.node ,[w,h]);
    },

    //设置lev
    setLevel( lev ){
        lev = lev || 1;
    },

    //射击出去
    emit(){
        const angle = this.node.rotation;
        let rigibody = this.node.getComponent(cc.RigidBody);
        // rigibody.active = true;
        rigibody.gravityScale = 0;
        
        var dx = 1200 * Math.cos( $T.toRota(90-angle) );
        var dy = 1200 * Math.sin( $T.toRota(90-angle) );
    //cc.Vec2
        let posss = cc.v2(dx,dy);//e.touch.getLocation();
        let velocity =  posss.normalize().mulSelf( 3000 );
        rigibody.linearVelocity = velocity;

        // setTimeout(() => {
        //    this.node.isValid && this.unuse() &&  $G.do('poolPutByNode' , 'put' , this.node );
        // }, 500);

        return this;
    },

    //当节点被再利用的时候。这个函数会被触发
    reuse:function (i) {
        $T.findChildNode("streak",this.node).active = true;
        return this;
    },

    //当节点被回收的时候，这个函数会被触发
    unuse:function () {
        // this.node.getComponent(cc.RigidBody).active = false;
        //$T.findChildNode("streak",this.node).active = false;
        
        let anim = this.childNode("anim");
        anim && (anim.scale=0);
        $T.findChildNode("streak",this.node).active = false;
        let rigibody = this.node.getComponent(cc.RigidBody);
        rigibody.linearVelocity = cc.v2(0,0);
        this.node.setPosition(cc.v2(0,0));
        this._killv = 0;
        return this;
    },

    playBoom( targ ){

        let node = $G.get('poolGetByName' , 'anode') || $RES.createPrefab("fab-anode");
        let frms = [$RES.g("img-zidan-boom") , $RES.g("img-zidan-boom1"), $RES.g("img-zidan-boom2")];
        node.parent = targ.parent;
        node.zIndex = 50;
        node.setPosition( targ.parent.convertToNodeSpaceAR(this.node.parent.convertToWorldSpaceAR(this.node.getPosition())) );

        this.unuse() &&  $G.do( 'poolPutByNode' , 'put' , this.node );
        $T.getScr(node).reuse().playFreameAnimate([
            {
                frame : frms[$T.random(1,frms.length)-1],
                size : [ 80 , ],
                scale : [ 0 , 1.3 ] , 
                time : 0.13,
                param : {},
                callfn : ()=>{}
            }
        ]).then(()=>{
            $T.getScr(node).unuse();
            $G.do('poolPutByNode' , "put" , node , "anode");
        });
    },

    update (dt) {
        const viruspanel = $G.get("viruspanel"),
              allvrs = viruspanel.get("allpos");
        
        let curpos = this.node.getPosition(),
            vpos , len;
        curpos = this.node.parent.convertToWorldSpaceAR( curpos );
        //console.log(curpos);
        if((curpos.x<-10 || curpos.x>800) || (curpos.x<-10 || curpos.x>1500) ){
            this.unuse() && $G.do('poolPutByNode' , 'put' , this.node );
            //console.log("%d||%d" , $G.get("pool-zidan")._pool.length , $G.get("pool-smzidan")._pool.length);
            //console.log( );
        }else
            for(let i in allvrs){
                vpos = allvrs[i].getPosition();
                vpos = allvrs[i].parent.convertToWorldSpaceAR(vpos);
                len = curpos.sub( vpos ).mag();
                if( len < (allvrs[i].width/2+this.node.height/2) ){
                    let rigibody = this.node.getComponent(cc.RigidBody);
                    let killv = this.getKillv();
                    rigibody.linearVelocity = cc.v2(0,0);
                    this.playBoom( allvrs[i] );
                    this.unuse() && $G.do('poolPutByNode' , 'put' , this.node );
                    $T.getScr(allvrs[i]).boom( killv );
                    break;
                }
            }
    }
});
